package cn.iocoder.yudao.module.member.service.jobView;

import cn.iocoder.yudao.module.member.api.jobView.dto.JobViewCreateReqDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.jobView.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.jobView.JobViewDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.member.convert.jobView.JobViewConvert;
import cn.iocoder.yudao.module.member.dal.mysql.jobView.JobViewMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 职位浏览记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class JobViewServiceImpl implements JobViewService {

    @Resource
    private JobViewMapper jobViewMapper;

    @Override
    public Long createJobView(JobViewCreateReqVO createReqVO) {
        // 插入
        JobViewDO jobView = JobViewConvert.INSTANCE.convert(createReqVO);
        jobViewMapper.insert(jobView);
        // 返回
        return jobView.getId();
    }

    @Override
    public void updateJobView(JobViewUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateJobViewExists(updateReqVO.getId());
        // 更新
        JobViewDO updateObj = JobViewConvert.INSTANCE.convert(updateReqVO);
        jobViewMapper.updateById(updateObj);
    }

    @Override
    public void deleteJobView(Long id) {
//        // 校验存在
//        this.validateJobViewExists(id);
        // 删除
        jobViewMapper.deleteById(id);
    }

    private void validateJobViewExists(Long id) {
        if (jobViewMapper.selectById(id) == null) {
            throw exception(JOB_VIEW_NOT_EXISTS);
        }
    }

    @Override
    public JobViewDO getJobView(Long id) {
        return jobViewMapper.selectById(id);
    }

    @Override
    public List<JobViewDO> getJobViewList(Collection<Long> ids) {
        return jobViewMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<JobViewDO> getJobViewPage(JobViewPageReqVO pageReqVO) {
        return jobViewMapper.selectPage(pageReqVO);
    }

    @Override
    public List<JobViewDO> getJobViewList(JobViewExportReqVO exportReqVO) {
        return jobViewMapper.selectList(exportReqVO);
    }

    @Override
    public long count(Long jobId, Long studentId) {
        Long aLong = jobViewMapper.selectCount(new QueryWrapper<JobViewDO>().eq(null!=studentId,"student_id", studentId).eq(jobId!=null,"job_id", jobId));
        return null==aLong?0:aLong;
    }

    @Override
    public void createJobView(@Valid JobViewCreateReqDTO jobViewCreateReqVO) {
        JobViewDO jobView=new JobViewDO();
        jobView.setViewTime(new Date());
        jobView.setCreator(jobViewCreateReqVO.getStudentId()+"");
        jobView.setUpdater(jobViewCreateReqVO.getStudentId()+"");
        BeanUtils.copyProperties(jobViewCreateReqVO,jobView);
        jobViewMapper.insert(jobView);
    }

    @Override
    public JobViewDO getJobView(Long jobId, Long studentId) {
        JobViewDO jobViewDO= jobViewMapper.selectOne(new QueryWrapper<JobViewDO>().eq(null!=studentId,"student_id", studentId)
                .last(" order by view_time asc limit 1"));
        return jobViewDO;
    }

    @Override
    public void updateJobView(@Valid JobViewCreateReqDTO jobViewCreateReqVO) {
        JobViewDO jobView=new JobViewDO();
        jobView.setViewTime(new Date());
        jobView.setUpdateTime(new Date());
        BeanUtils.copyProperties(jobViewCreateReqVO,jobView);
        jobViewMapper.update(jobView,new QueryWrapper<JobViewDO>().eq("student_id", jobView.getStudentId()).eq("job_id", jobView.getJobId()));
    }
}
